package com.graduate.examapp.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cn.hutool.core.util.ArrayUtil
import com.blankj.utilcode.util.ArrayUtils
import com.blankj.utilcode.util.CollectionUtils
import com.graduate.examapp.adapter.TeacherRecycleViewAdapter
import com.graduate.examapp.entity.ExamRangeParam
import com.graduate.examapp.entity.ExamTask
import com.graduate.examapp.entity.UserInfoFactory
import com.graduate.examapp.model.ExamTaskRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.FieldPosition
import java.util.Arrays

class RangeViewModel : ViewModel() {
    val rangeSuccessLiveData = MutableLiveData<String>()
    //val rangeResultLiveData = MutableLiveData<ExamTask?>()
    val rangeLiveData = MutableLiveData<MutableList<ExamTask>?>()

    private val rangeRepository = ExamTaskRepository()

    fun rangeExam(examRangeParam: ExamRangeParam) {
        Log.d("RangeViewModel", examRangeParam.toString())
        viewModelScope.launch(Dispatchers.IO) {
            val response = rangeRepository.rangeExam(examRangeParam)
            if (response.code == LoginViewModel.REQUEST_OK) {
                //向recycleView中插入数据
                //rangeResultLiveData.postValue(response.data)
                UserInfoFactory.loginData?.insName?.let {
                    fetchAllExamTask(it)
                }
                rangeSuccessLiveData.postValue("排考成功")
            } else {
                rangeSuccessLiveData.postValue(response.message)
            }
        }
    }

    fun fetchAllExamTask(insName: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val examTaskResponse = rangeRepository.fetchAllExamTask(insName)
            if (examTaskResponse.code == LoginViewModel.REQUEST_OK) {
                Log.d("RangeViewModel", examTaskResponse.data.toString())
                rangeLiveData.postValue(examTaskResponse.data)
            } else {
                rangeSuccessLiveData.postValue(examTaskResponse.message)
            }
        }
    }

    fun cancelExam(id: Int, position: Int) {
        Log.d("RangeViewModel", id.toString())
        viewModelScope.launch(Dispatchers.IO) {
            val delExamResponse = rangeRepository.delExamTask(id)
            if (delExamResponse.code == LoginViewModel.REQUEST_OK) {
                rangeSuccessLiveData.postValue("撤销成功")
                UserInfoFactory.loginData?.insName?.let {
                    fetchAllExamTask(it)
                }
            } else {
                rangeSuccessLiveData.postValue(delExamResponse.message)
            }
        }
    }

    fun setFirstDay(firstday: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val response = rangeRepository.setFirstDay(firstday)
            if (response.code == LoginViewModel.REQUEST_OK) {
                rangeSuccessLiveData.postValue("设置成功")
            } else {
                rangeSuccessLiveData.postValue(response.message)
            }
        }
    }
}